package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.OrderBean;
import com.project.bean.UserBean;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @RequestMapping("add")
    public String add(OrderBean order, HttpServletRequest req){
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        order.setUserId(user.getId());
        orderService.add(order);
        return "ok";
    }

    @RequestMapping("cutByItem")
    public IPage<OrderBean> cutByItem(Integer pageNO, String status, LocalDate startDate,LocalDate endDate){
        return orderService.cutByItem(pageNO,null,status,startDate,endDate);
    }

    @RequestMapping("cutByItemInUser")
    public IPage<OrderBean> cutByItemInUser(Integer pageNO, Integer userId, String status,
                                            LocalDate startDate,LocalDate endDate,
                                            HttpServletRequest req){
        UserBean loginUser=(UserBean) req.getSession().getAttribute("loginUser");
        return orderService.cutByItem(pageNO,loginUser.getId(),status,startDate,endDate);
    }

    @RequestMapping("findByStatus")
    public List<OrderBean> findByStatus(){
        return orderService.findByStatus();
    }

    @RequestMapping("findById")
    public OrderBean findById(Integer id){
        return orderService.findById(id);
    }

    @RequestMapping("send")
    public String send(Integer id){
        orderService.send(id);
        return "ok";
    }
    @RequestMapping("complete")
    public String complete(Integer id){
        orderService.complete(id);
        return "ok";
    }
}
